package com.qls.mt.biz.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.qls.core.constant.Constant;
import com.qls.core.dto.PushHotelBody;
import com.qls.core.dto.PushRatesBody;
import com.qls.core.dto.PushRpBody;
import com.qls.core.entity.DtOrderChannels;
import com.qls.core.entity.HtHotel;
import com.qls.core.utils.R;
import com.qls.mt.biz.common.service.DtOrderChannelsService;
import com.qls.mt.biz.common.service.HtHotelinfoService;
import com.qls.mt.biz.common.service.OpHtlGroupService;
import com.qls.mt.biz.common.utils.MtResponse;
import com.qls.mt.biz.data.service.CallBackService;
import com.qls.mt.biz.data.service.InsertHtService;
import com.qls.rabbitmq.SendMsg;
import com.qls.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @author Administrator
 */
@RestController
@Slf4j
public class CallBackController {

    @Autowired
    private CallBackService callBackService;

    @Autowired
    private HtHotelinfoService htHotelinfoService;

    @Autowired
    private InsertHtService insertHtService;

    @Autowired
    private DtOrderChannelsService dtOrderChannelsService;

    @Autowired
    private OpHtlGroupService opHtlGroupService;

    @Autowired
    private SendMsg sendMsg;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 接收美团发送的房态变化
     *
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "callback/rs")
    public MtResponse rs(@RequestBody JSONObject jsonObject) {
        try {
            callBackService.rs(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return MtResponse.error();
        }
        return MtResponse.ok();
    }

    /**
     * 接收美团价格政策变化信息
     *
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "callback/rp")
    public MtResponse rp(@RequestBody JSONObject jsonObject) {
        try {
            callBackService.rp(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return MtResponse.error();
        }
        return MtResponse.ok();
    }

    /**
     * 接受美团酒店变化信息
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "callback/hotel", method = RequestMethod.POST)
    public MtResponse poi(@RequestBody JSONObject jsonObject) {
        try {
            callBackService.poi(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return MtResponse.error();
        }
        return MtResponse.ok();
    }

    /**
     * 接受美团酒店变化信息
     *
     * @param htHotel
     * @return
     */
    @RequestMapping(value = "hotel/CALLBACK_HOTEL", method = RequestMethod.POST)
    public R callbackHotel(@RequestBody HtHotel htHotel) {
        Long hotelId = htHotel.getId();
        Integer partnerId = htHotel.getPartnerId();
        HtHotel hotel = htHotelinfoService.getById(hotelId);
        PushHotelBody pushHotelBody = new PushHotelBody();
        pushHotelBody.setHotelId(hotelId);
        if (Objects.isNull(hotel)) {
            DtOrderChannels dtOrderChannels = dtOrderChannelsService.getByPartnerId(partnerId);
            Map<String, Object> map = new HashMap<>();
            map.put("partnerId", partnerId);
            map.put("accessKey", dtOrderChannels.getAccessKey());
            map.put("secretKey", dtOrderChannels.getSecret());
            insertHtService.insertHtHotelinfo(pushHotelBody, map);
        } else {
            hotel.setHotelStatus(htHotel.getHotelStatus());
            htHotelinfoService.saveOrUpdate(hotel);
            List<Integer> stores = opHtlGroupService.getStoreIdByHotelId(hotelId);
            pushHotelBody.setType(2);
            for (Integer storeId : stores) {
                pushHotelBody.setStoreId(storeId);
                sendMsg.execute(Constant.RabbitMq.HOTEL, storeId, pushHotelBody);
            }
        }
        return R.ok();
    }

    /**
     * 接受美团房态变化和价格信息
     *
     * @param pushRatesBody
     * @return
     */
    @RequestMapping(value = "hotel/CALLBACK_RATES", method = RequestMethod.POST)
    public R callbackRates(@RequestBody PushRatesBody pushRatesBody) {
        Map<String, Object> stores = (Map<String, Object>) redisUtil.hashGet(Constant.Redis.RP_BL_STORE.getValue(), pushRatesBody.getRpCode().toString());
        List<Integer> storeIds = (List<Integer>) stores.get("storeIds");
        for (Integer store : storeIds) {
            pushRatesBody.setStoreId(store);
            sendMsg.execute(Constant.RabbitMq.RATES, store, pushRatesBody);
        }
        return R.ok();
    }

    /**
     * 接受美团售卖房型信息
     *
     * @param pushRpBody
     * @return
     */
    @RequestMapping(value = "hotel/CALLBACK_RP", method = RequestMethod.POST)
    public R callbackRp(@RequestBody PushRpBody pushRpBody) {
        Map<String, Object> stores = (Map<String, Object>) redisUtil.hashGet(Constant.Redis.RP_BL_STORE.getValue(), pushRpBody.getRpCode().toString());
        List<Integer> storeIds = (List<Integer>) stores.get("storeIds");
        for (Integer store : storeIds) {
            pushRpBody.setStoreId(store);
            sendMsg.execute(Constant.RabbitMq.RP, store, pushRpBody);
        }
        return R.ok();
    }

}
